package com.oromwin.repos.server.response.volume;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.Logger;
import com.oromwin.repos.common.ReposException;
import com.oromwin.repos.common.helper.IOHelper;
import com.oromwin.repos.data.AbstractReposTable;
import com.oromwin.repos.data.MediaTable;
import com.oromwin.repos.server.response.NormalResponse;

public class ListVolumeResponse extends NormalResponse {
    private Log log = Logger.getLog(ListVolumeResponse.class);
    private List<AbstractReposTable> ReposTables = null;
    
    public ListVolumeResponse(boolean result, String description, List<AbstractReposTable> reposTables) {
        Result = result;
        Description = description;
        ReposTables = reposTables;
    }
    
    public void send(DataOutputStream dos) throws ReposException {
        try {
            dos.writeBoolean(Result);
            IOHelper.writeUTF8(dos, Description.toString());
            dos.writeInt(ReposTables.size());
            for (AbstractReposTable abstractTable : ReposTables) {
                if (abstractTable instanceof MediaTable) {
                    MediaTable mediaTable = (MediaTable) abstractTable;
                    IOHelper.writeUTF8(dos, mediaTable.getVolumeId());
                    IOHelper.writeUTF8(dos, mediaTable.getMediaId());
                    IOHelper.writeUTF8(dos, mediaTable.getMediaPath().toString());
                    dos.writeLong(mediaTable.getMediaMaxBytes());
                    dos.writeLong(mediaTable.getMediaUsedBytes());
                    dos.writeInt(mediaTable.getMediaFileCount());
                    dos.writeBoolean(mediaTable.getMediaStatus());
                    IOHelper.writeUTF8(dos, mediaTable.getMediaCreateDate());
                    IOHelper.writeUTF8(dos, mediaTable.getMediaUpdateDate());
                } else {
                    throw ReposException.create(log, String.format("Illegal AbstractTable type, {%s}", abstractTable.getClass().toString()));
                }
            }
        } catch (IOException e) {
            throw ReposException.create(log, e);
        }
    }
}
